<!-- pages/login/components/mobile.vue -->
<script setup>
import { getCodeApi, loginByMobileApi } from '@/apis/user';
import { http } from '@/utils/http';
import { ref } from 'vue'
import { useUserStore } from '@/stores/user_store';
const store = useUserStore()
const isCountDown = ref(false)
const formRef = ref(null)
// 获取验证码
const getMobileCode = async () => {
    // 校验手机号
    await formRef.value.validateField(['mobile'])
    // 获取验证码
    const res = await getCodeApi({ mobile: formData.value.mobile, type: 'login' })
    uni.showToast({ title: '验证码已发送', icon: 'none' })
    formData.value.code = res.code
    // 开始倒计时
    isCountDown.value = true
}
// 是否勾选同意
const isAgress = ref(false)
// 表单数据
const formData = ref({
    mobile: '13211112222',
    code: '',
})
// 表单校验
const rules = {
    mobile: {
        rules: [
            { required: true, errorMessage: '请填写手机号码' },
            { pattern: '^1\\d{10}$', errorMessage: '手机号码格式不正确' },
        ],
    },
    code: {
        rules: [
            {
                required: true,
                errorMessage: '请输入验证码',
            },
            {
                pattern: '^\\d{6}$',
                errorMessage: '验证码格式不正确',
            },
        ],
    },
}
// 提交
const onSubmit = async () => {
    try {
        if (!isAgress.value) return uni.showToast({ title: '请勾选同意用户协议', icon: 'none' })
        // 验证通过后会返回表单的数据
        const data = await formRef.value.validate()
        console.log(data)
        // 登录接口
        const res = await loginByMobileApi({ mobile: data.mobile, code: data.code })
        console.log(res);
        store.setToken(res.token)
        uni.showToast({ title: '登录成功' })
        uni.switchTab({
            url: '/pages/my/index',
        })
    } catch (error) {
        console.log(error)
    }
}
</script>

<template>
    <uni-forms class="login-form" ref="formRef" :model-value="formData" :rules="rules">
        <uni-forms-item name="mobile">
            <uni-easyinput :input-border="false" :clearable="false" placeholder="请输入手机号"
                placeholder-style="color: #C3C3C5" v-model="formData.mobile" />
        </uni-forms-item>
        <uni-forms-item name="code">
            <uni-easyinput :input-border="false" :clearable="false" placeholder="请输入验证码"
                placeholder-style="color: #C3C3C5" v-model="formData.code" />
            <text class="text-button" v-if="!isCountDown" @click="getMobileCode">获取验证码</text>
            <!-- 倒计时 -->
            <view v-else>
                <uni-countdown :second="59" @timeup="isCountDown = false" />秒后重新获取
            </view>
        </uni-forms-item>

        <view class="agreement">
            <radio :checked="isAgress" color="#16C2A3" @click="isAgress = !isAgress" />
            我已同意
            <text class="link">用户协议</text>
            及
            <text class="link">隐私协议</text>
        </view>

        <button class="uni-button" @click="onSubmit">登 录</button>
    </uni-forms>
</template>

<script>
export default {
    options: {
        styleIsolation: 'shared',
    },
}
</script>

<style lang="scss">
@import './styles.scss';
</style>
